﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Pigs.Weapons
{
    public class Flame : pigs.DynamicObject
    {
        // Position of the flame relative to the upper left side of the screen
        public Vector2 Direction;

        // For flipping flame when reflected
        public SpriteEffects spriteeffect;

        //angle that it's fired at
        public float angle;

        int timer;

        // Determines how fast the projectile moves
        Vector2 flameSpeed;

        /// <summary>
        /// create the flame
        /// </summary>
        /// <param name="texture">texture to use for the flame</param>
        /// <param name="position">where to draw the flame</param>
        public Flame(Texture2D texture, Vector2 position)
            : base(texture, position)
        {
        }

        /// <summary>
        /// initialize the flame
        /// </summary>
        /// <param name="direction">vector of the position it's going to</param>
        public void Initialize(Vector2 direction)
        {
            this.Direction = direction;
            this.spriteeffect = SpriteEffects.None;

            Random rand = new Random();
            //calculate the angle from the direction it's going
            this.angle = (float)(Math.Atan2(Direction.Y, Direction.X));
            this.damage = 0.015f;
            this.timer = 70;

            this.flameSpeed = new Vector2((float)Math.Cos(angle) * 8, (float)Math.Sin(angle) * 8);

            this.flameSpeed.X += rand.Next(-1, 1);
            this.flameSpeed.Y += rand.Next(-1, 1);

            this.rectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
        }

        /// <summary>
        /// update the flames position
        /// </summary>
        public void Update()
        {
            // Projectiles always move to the right
            position += flameSpeed;
            if (flameSpeed.X > 0)
            {
                this.flameSpeed.X -= 0.05f;
            }
            else if (flameSpeed.X < 0)
            {
                this.flameSpeed.X += 0.05f;
            }
            if (flameSpeed.Y > -5)
            {
                flameSpeed.Y -= 0.1f;
            }
            else if (flameSpeed.Y < -5)
            {
                flameSpeed.Y += 0.1f;
            }

            if (this.timer > 0)
            {
                this.timer -= 1;
            }
            else
            {
                this.active = false;
            }
            rectangle = new Rectangle((int)position.X, (int)position.Y, rectangle.Width, rectangle.Height);
        }

        /// <summary>
        /// draw the flame
        /// </summary>
        /// <param name="spriteBatch">the spritebatch to draw the flame in</param>
        public override void Draw(SpriteBatch spriteBatch)
        {
            if (timer > 10)
            {
                spriteBatch.Draw(texture, position, null, Color.White, 0, new Vector2(texture.Width / 2, texture.Height / 2), 1f, spriteeffect, 0f);
            }
            else
            {
                float scale = (float)(timer) / 10f;
                spriteBatch.Draw(texture, position, null, Color.White, 0, new Vector2(texture.Width / 2, texture.Height / 2), scale, spriteeffect, 0f);
            }
        }
    }
}
